<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class vt extends MY_Controller {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->lang->load('vt');
    }
    
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
        //$this->template->set_regions('header','<h3>Hearder ne con</h3>');
        //$this->template->set_regions('footer','<h3>Footer ne con</h3>');
        //echo "ssssss";
        //$this->template->set('bien','1002');
        if( !has_permission('KHVT.Product.PageView') ){
            redirect('no_access');
        }
        $this->load->model('vt_model');
        $categories = $this->vt_model->getAllCategory();
        $this->template->set('categories',$categories);
        
        $this->template->set_page_title( lang('title_quanlyvattu') );
        $this->template->set_view('vt/content_vt');        
        $this->template->render();
		//$this->load->view('welcome_message');
	}
    
    public function datatable_vt(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_vt($_GET,1);
        $totalData = $this->vt_model->load_datatable_vt($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    public function ajax_loadfrm_vt($id = null){
        $this->load->model('vt_model');
        $data = array();
        
        $data['dvt'] = $this->vt_model->getAllDVT();
        $data['category'] = $this->vt_model->getAllCategory();
        
        if($id != null){
            $data['vt'] = $this->vt_model->getVTById($id);
            $data['active'] = site_url('vt/edit_vt');
        }else{
            $data['active'] = site_url('vt/add_vt');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/vt/form_add_vt',$data);
    }
    public function add_vt(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Product.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $code = trim( $this->input->post('code') );
            $name = trim( $this->input->post('name') );
            $total = trim( $this->input->post('total') );
            $price = trim( $this->input->post('price') );
            $unit_id = $this->input->post('unit_id');
            $catg_id = $this->input->post('catg_id');
            $status = $this->input->post('status');
            
            $flag = true;
            $id = 0;
            $error = array();
            
            if( $code == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_001'),
                    'name' => 'code'
                );
            }else{
                if( $this->vt_model->unique_code($code) == false ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_002'),
                        'name' => 'code'
                    );
                }
            }
            
            if($name == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_003'),
                    'name' => 'name'
                );
            }
            
            if($total == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_004'),
                    'name' => 'total'
                );
            }else{
                if(!numeric_check($total)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_005'),
                        'name' => 'total'
                    );
                }else{
                    if($total <= 0){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('vt_error_006'),
                            'name' => 'total'
                        );
                    }
                }
            }
            
            if($price == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_007'),
                    'name' => 'price'
                );
            }else{
                if(!numeric_check($price)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_008'),
                        'name' => 'price'
                    );
                }else{
                    if($price <= 0){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('vt_error_009'),
                            'name' => 'price'
                        );
                    }
                }
            }
            
            if($unit_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_010'),
                    'name' => 'unit_id'
                );
            }
            if($catg_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_011'),
                    'name' => 'catg_id'
                );
            }
            
            if($flag == true){
                $data_insert = array(
                    'code' => $code,
                    'name' => $name,
                    'total' => $total,
                    'price' => $price,
                    'unit_id' => $unit_id,
                    'catg_id' => $catg_id,
                    'status' => $status
                );
                
                $id = $this->vt_model->insertVT($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'code' => $code,
                'name' => $name,
                'total' => $total,
                'unit_id' => $unit_id,
                'catg_id' => $catg_id,
                'status' => $status
            );
            echo json_encode($data);
            
        }
    }
    public function edit_vt(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Product.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $code = trim( $this->input->post('code') );
            $name = trim( $this->input->post('name') );
            $total = trim( $this->input->post('total') );
            $price = trim( $this->input->post('price') );
            $unit_id = $this->input->post('unit_id');
            $catg_id = $this->input->post('catg_id');
            $status = $this->input->post('status');
            
            $flag = true;
            $error = array();
            
            if( $code == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_001'),
                    'name' => 'code'
                );
            }else{
                if( $this->vt_model->unique_code_edit($id,$code) == false ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_002'),
                        'name' => 'code'
                    );
                }
            }
            
            if($name == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_003'),
                    'name' => 'name'
                );
            }
            
            if($total == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_004'),
                    'name' => 'total'
                );
            }else{
                if(!numeric_check($total)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_005'),
                        'name' => 'total'
                    );
                }else{
                    if($total <= 0){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('vt_error_006'),
                            'name' => 'total'
                        );
                    }
                }
            }
            
            if($price == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_007'),
                    'name' => 'price'
                );
            }else{
                if(!numeric_check($price)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_008'),
                        'name' => 'price'
                    );
                }else{
                    if($price <= 0){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('vt_error_009'),
                            'name' => 'price'
                        );
                    }
                }
            }
            
            if($unit_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_010'),
                    'name' => 'unit_id'
                );
            }
            if($catg_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_011'),
                    'name' => 'catg_id'
                );
            }
            
            if($flag == true){
                $data_update = array(
                    'code' => $code,
                    'name' => $name,
                    'total' => $total,
                    'price' => $price,
                    'unit_id' => $unit_id,
                    'catg_id' => $catg_id,
                    'status' => $status
                );
                $this->vt_model->updateVT($data_update,$id);
            }
            $vt = $this->vt_model->getVTById($id);
            $html = '';
            if($vt->status == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
            }elseif($vt->status == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
            }elseif($vt->status == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
            }
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'code' => $vt->code,
                'name' => $vt->name,
                'total' => $vt->total,
                'price' => $vt->price,
                'total_price' => $vt->price * $vt->total,
                'unit_id' => $vt->val_name,
                'catg_id' => $vt->catg_name,
                'status' => $html
            );
            echo json_encode($data);
            
        }
    }
    public function delete_vt(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Product.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data_update = array('status' => -1);
            $this->vt_model->updateVT($data_update,$id);
            $data = array(
                'flag' => true,
                'message' => 'delete thanh cong',
            );
            echo json_encode($data);
        }
    }
    /*
    * Don Vi Tinh
    */
    public function dv()
    {
        //$this->template->set_regions('header','<h3>Hearder ne con</h3>');
        //$this->template->set_regions('footer','<h3>Footer ne con</h3>');
        //echo "ssssss";
        
        //$this->template->set('bien','1002');
        if( !has_permission('KHVT.Value.PageView') ){
            redirect('no_access');
        }
        $this->template->set_page_title( lang('menu_donvitinh') );
        $this->template->set_view('vt/content_dvt');
        
        $this->template->render();
        
        
        //$this->load->view('welcome_message');
    }
    
    public function datatable_dvt(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_dvt($_GET,1);
        $totalData = $this->vt_model->load_datatable_dvt($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_dvt($id = null){
        $this->load->model('vt_model');
        $data = array();
        
        if($id != null){
            $data['dvt'] = $this->vt_model->getValueById($id);
            $data['active'] = site_url('vt/edit_dvt');
        }else{
            $data['active'] = site_url('vt/add_dvt');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/vt/form_add_dvt',$data);
    }
    
    public function add_dvt(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Value.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $name = trim( $this->input->post('name') );
            $status = $this->input->post('status');
            
            $flag = true;
            $id = 0;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_013'),
                    'name' => 'name'
                );
            }
            
            if($flag == true){
                $data_insert = array(
                    'name' => $name,
                    'status' => $status
                );
                
                $id = $this->vt_model->insertDVT($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'name' => $name,
                'status' => $status
            );
            echo json_encode($data);
        }
    }
    
    public function edit_dvt(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Value.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $name = trim( $this->input->post('name') );
            $status = $this->input->post('status');
            
            $flag = true;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_013'),
                    'name' => 'name'
                );
            }
            
            if($flag == true){
                $data_update = array(
                    'name' => $name,
                    'status' => $status
                );
                $this->vt_model->updateDVT($data_update,$id);
            }
            
            $dvt = $this->vt_model->getValueById($id);
                
            $html = '';
            if($dvt->status == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
            }elseif($dvt->status == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
            }elseif($dvt->status == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
            }
                    
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'name' => $dvt->name,
                'status' => $html
            );
            echo json_encode($data);
        }
    }
    
    public function delete_dvt(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Value.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data_update = array('status' => -1);
            $this->vt_model->updateDVT($data_update,$id);
            $data = array(
                'flag' => true,
                'message' => 'delete thanh cong',
            );
            echo json_encode($data);
        }
    }
    
    /*
    * Phan Loai Vat Tu
    */
    public function pl()
    {
        if( !has_permission('KHVT.Category.PageView') ){
            redirect('no_access');
        }
        $this->template->set_page_title( lang('menu_ploaivattu') );        
        $this->template->set_view('vt/content_pl');        
        $this->template->render();
    }
    
    public function datatable_pl(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_pl($_GET,1);
        $totalData = $this->vt_model->load_datatable_pl($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_pl($id = null){
        $this->load->model('vt_model');
        $data = array();
        
        if($id != null){
            $data['pl'] = $this->vt_model->getCatgById($id);
            $data['active'] = site_url('vt/edit_pl');
        }else{
            $data['active'] = site_url('vt/add_pl');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/vt/form_add_pl',$data);
    }
    
    public function add_pl(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Category.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $name = trim( $this->input->post('name') );
            $status = $this->input->post('status');
            
            $flag = true;
            $id = 0;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_012'),
                    'name' => 'name'
                );
            }
            
            if($flag == true){
                $data_insert = array(
                    'name' => $name,
                    'status' => $status
                );
                
                $id = $this->vt_model->insertPL($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'name' => $name,
                'status' => $status
            );
            echo json_encode($data);
        }
    }
    
    public function edit_pl(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Category.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $name = trim( $this->input->post('name') );
            $status = $this->input->post('status');
            
            $flag = true;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_012'),
                    'name' => 'name'
                );
            }
            
            if($flag == true){
                $data_update = array(
                    'name' => $name,
                    'status' => $status
                );
                $this->vt_model->updatePL($data_update,$id);
            }
            
            $pl = $this->vt_model->getCatgById($id);
                
            $html = '';
            if($pl->status == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
            }elseif($pl->status == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
            }elseif($pl->status == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
            }
                    
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'name' => $pl->name,
                'status' => $html
            );
            echo json_encode($data);
        }
    }
    
    public function delete_pl(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Category.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data_update = array('status' => -1);
            $this->vt_model->updatePL($data_update,$id);
            $data = array(
                'flag' => true,
                'message' => 'delete thanh cong',
            );
            echo json_encode($data);
        }
    }
    
    public function supplier()
    {
        if( !has_permission('KHVT.Supplier.PageView') ){
            redirect('no_access');
        }
        $this->template->set_page_title( lang('menu_nhacungcap') );
        $this->template->set_view('vt/content_supplier');
        $this->template->render();
    }
    
    public function datatable_supplier(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_supplier($_GET,1);
        $totalData = $this->vt_model->load_datatable_supplier($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_supplier($id = null){
        $this->load->model('vt_model');
        $data = array();
        
        if($id != null){
            $data['supp'] = $this->vt_model->getSupplierById($id);
            $data['active'] = site_url('vt/edit_supplier');
        }else{
            $data['active'] = site_url('vt/add_supplier');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/vt/form_add_supplier',$data);
    }
    
    public function add_supplier(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Supplier.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $supplier_name = trim( $this->input->post('supplier_name') );
            $supplier_note = $this->input->post('supplier_note');
            $status = $this->input->post('status');
            
            $flag = true;
            $id = 0;
            $error = array();
            
            if( $supplier_name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_014'),
                    'name' => 'supplier_name'
                );
            }
            
            if($flag == true){
                $data_insert = array(
                    'supplier_name' => $supplier_name,
                    'supplier_note' => $supplier_note,
                    'status' => $status
                );
                $id = $this->vt_model->insertSupplier($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'supplier_name' => $supplier_name,
                'supplier_note' => $supplier_note,
                'status' => $status
            );
            echo json_encode($data);
        }
    }
    
    public function edit_supplier(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Supplier.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $supplier_name = trim( $this->input->post('supplier_name') );
            $supplier_note = $this->input->post('supplier_note');
            $status = $this->input->post('status');
            
            $flag = true;
            $error = array();
            
            if( $supplier_name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_014'),
                    'name' => 'supplier_name'
                );
            }
            
            if($flag == true){
                $data_update = array(
                    'supplier_name' => $supplier_name,
                    'supplier_note' => $supplier_note,
                    'status' => $status
                );
                $this->vt_model->updateSupplier($data_update,$id);
            }
            
            $supp = $this->vt_model->getSupplierById($id);
                
            $html = '';
            if($supp->status == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
            }elseif($supp->status == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
            }elseif($supp->status == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
            }
                    
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'supplier_name' => $supp->supplier_name,
                'supplier_note' => $supp->supplier_note,
                'status' => $html
            );
            echo json_encode($data);
        }
    }
    
    public function delete_supplier(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Supplier.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data_update = array('status' => -1);
            $this->vt_model->updateSupplier($data_update,$id);
            $data = array(
                'flag' => true,
                'message' => 'delete thanh cong',
            );
            echo json_encode($data);
        }
    }
    
    public function construction(){
        if( !has_permission('KHVT.Construction.PageView') ){
            redirect('no_access');
        }
        $this->template->set_page_title( lang('menu_qlycongtrinh') );
        $this->template->set_view('vt/content_construction');
        $this->template->render();
    }
    
    public function datatable_construction(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_construction($_GET,1);
        $totalData = $this->vt_model->load_datatable_construction($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_construction($id = null){
        $this->load->model('vt_model');
        $data = array();
        
        if($id != null){
            $data['construction'] = $this->vt_model->getConstructionById($id);
            $data['active'] = site_url('vt/edit_construction');
        }else{
            $data['active'] = site_url('vt/add_construction');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/vt/form_add_construction',$data);
    }
    
    public function add_construction(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Construction.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $name = trim( $this->input->post('name') );
            
            $flag = true;
            $id = 0;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_015'),
                    'name' => 'name'
                );
            }
            
            if($flag == true){
                $data_insert = array(
                    'name' => $name,
                    'created_date' => date("Y-m-d H:i:s"),
                    'end_date' => null,
                    'status' => 1
                );
                
                $id = $this->vt_model->insertConstruction($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'name' => $name
            );
            echo json_encode($data);
        }
    }
    
    public function edit_construction(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Construction.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $name = trim( $this->input->post('name') );

            $flag = true;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_015'),
                    'name' => 'name'
                );
            }
            
            if($flag == true){
                $data_update = array(
                    'name' => $name
                );
                $this->vt_model->updateConstruction($data_update,$id);
            }
            
            $cons = $this->vt_model->getConstructionById($id);
            
            $html = '';
            if($cons->status == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="Delete">';
            }elseif($cons->status == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="Dang thi cong">';
            }elseif($cons->status == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="Ket thuc">';
            }
                    
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'name' => '<a href="'.site_url('vt/construction_view/'.$id).'">'.$cons->name.'</a>',
                'status' => $html
            );
            echo json_encode($data);
        }
    }
    
    public function delete_construction(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Construction.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data_update = array('status' => -1);
            $this->vt_model->updateConstruction($data_update,$id);
            $data = array(
                'flag' => true,
                'message' => 'delete thanh cong',
            );
            echo json_encode($data);
        }
    }
    
    public function construction_view($id){
        $this->load->model('vt_model');
        
        $construction = $this->vt_model->getConstructionById($id);
        $this->template->set('construction',$construction);
        
        $export_data = $this->vt_model->getExportBillByConstruction($id);
        $this->template->set('export_data',$export_data);
        
        $products = $this->vt_model->getProductExportByConstruction($id);
        $this->template->set('products',$products);
        
        $this->template->set_page_title( lang('title_constructionview') );
        $this->template->set_view('construction/content_view');
        
        $this->template->render();
    }
    
    public function end_construction(){
        $this->load->model('vt_model');
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $this->vt_model->updateConstruction(array('status' => 2,'end_date'=> date("Y-m-d H:i:s")),$id);
            
            redirect('vt/construction_view/'.$id);
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */